#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import json

dir_path = '/mnt5/contact_team/dsf/week_data'
os.chdir(dir_path)

sources = [
    'b2b', 'bidding', 'qcc', 'recruiting', 'year'
]

week_report_result_path = os.path.join(dir_path, 'week_report')

head_line = [
    '', '周更新库内联系方式总量', '周新增库内联系方式总量', '周更新库内手机总量', '周新增库内手机总量', '周绝对新增库内手机总量', '在营企业手机周更新', '在营企业手机周新增', '在营企业手机周绝对新增',
    '该表新增条数', '该表更新条数', '手机号新增条数', '手机号更新条数', '有手机号有nameId新增条数', '有手机号有nameId更新条数', '入联系方式表条数'
]
rows = []
with open(week_report_result_path, 'r', encoding='utf-8') as f:
    week_data_json = json.load(f)
    # 处理总表
    all_table_head_line = [
        '时间周期','总量','近一个月更新数量','近二个月更新数量','近三个月更新数量','周更新数量','周新增数量'
    ]
    rows.append(all_table_head_line)
    rows.append(['库内总量：',week_data_json['all_table_count'],'','','',week_data_json['all_update'],week_data_json['all_add']]) # 库内联系方式总量

    rows.append(['手机号总量（contact）：', week_data_json['table_mobile_count_dup']]) # 库内手机号总量
    rows.append(['手机条数总量（nameId+contact）：',week_data_json['table_mobile_count'], week_data_json['one_month_mobile_update'],
                 week_data_json['two_month_mobile_update'],week_data_json['three_month_mobile_update'],
                 week_data_json['mobile_update'],week_data_json['mobile_add']
                 ]) # 库内手机条数总量
    rows.append(['映射企业数量（去重）：',week_data_json['table_mobile_count_ent_dup'],week_data_json['one_month_mobile_update_dup'],
                 week_data_json['two_month_mobile_update_dup'],week_data_json['three_month_mobile_update_dup'],
                 week_data_json['mobile_update_dup'],week_data_json['mobile_add_dup']
                 ]) # 库内手机映射企业数量

    rows.append(['周绝对新增手机数（contact）：',week_data_json['mobile_abs_add']])
    rows.append(['周新增企业数量（去重）：',week_data_json['mobile_abs_add_ent_dup']])



    rows.append(['固话号总量（contact）：',week_data_json['table_phone_count_dup']])
    rows.append(['固话条数总量（nameId+contact）：',week_data_json['table_phone_count'],
                week_data_json['one_month_phone_update'],
                 week_data_json['two_month_phone_update'],week_data_json['three_month_phone_update'],
                 week_data_json['phone_update'],week_data_json['phone_add']]

                )


    rows.append(['映射企业数量（去重）',week_data_json['table_phone_count_ent_dup'],
                week_data_json['one_month_phone_update_dup'],
                 week_data_json['two_month_phone_update_dup'],week_data_json['three_month_phone_update_dup'],
                 week_data_json['phone_update_dup'],week_data_json['phone_add_dup']]
    )
    rows.append(['周绝对新增固话数（contact）：', week_data_json['phone_abs_add']])
    rows.append(['周新增企业数量（去重）：', week_data_json['phone_abs_add_ent_dup']])

    #
    rows.append(['邮箱号总量（contact）：', week_data_json['table_email_count_dup']])
    rows.append(['邮箱条数总量（nameId+contact）：', week_data_json['table_email_count'],
                 week_data_json['one_month_email_update'],
                 week_data_json['two_month_email_update'], week_data_json['three_month_email_update'],
                 week_data_json['email_update'], week_data_json['email_add']]

                )

    rows.append(['映射企业数量（去重）', week_data_json['table_email_count_ent_dup'],
                 week_data_json['one_month_email_update_dup'],
                 week_data_json['two_month_email_update_dup'], week_data_json['three_month_email_update_dup'],
                 week_data_json['email_update_dup'], week_data_json['email_add_dup']]
                )
    rows.append(['周绝对新增邮箱数（contact）：', week_data_json['email_abs_add']])
    rows.append(['周新增企业数量（去重）：', week_data_json['email_abs_add_ent_dup']])


    rows.append(['在营企业数量：', week_data_json['zy_enterprise_count']])
    rows.append(['在营企业手机条数（nameId+contact）：', week_data_json['zy_enterprise_mobile_count'],
                 week_data_json['one_month_zy_enterprise_mobile_update'],week_data_json['two_month_zy_enterprise_mobile_update'],
                 week_data_json['three_month_zy_enterprise_mobile_update'],week_data_json['week_zy_enterprise_mobile_update'],
                 week_data_json['week_zy_enterprise_mobile_add']
                 ])
    rows.append(['周绝对新增数量（contact）：', week_data_json['week_zy_enterprise_mobile_abs_add']])





    # 处理细项源
    rows.append(head_line)
    source_result_dic = week_data_json['source_result_dic']
    for k, v in source_result_dic.items():
        source_name = k
        table_update_count = v['table_update_count']  # ** 周更新库内联系方式总量：{} **
        table_add_count = v['table_add_count']  # ** 周新增库内联系方式总量：{} **
        mobile_update = v['mobile_update']  # 周更新库内手机总量
        mobile_add = v['mobile_add']  # 周新增库内手机总量
        mobile_abs_add = v['mobile_abs_add']  # 周绝对新增库内手机总量
        zy_mobile_update = v['zy_mobile_update']  # 在营企业手机周更新
        zy_mobile_add = v['zy_mobile_add']  # 在营企业手机周新增
        zy_mobile_abs_add = v['zy_mobile_abs_add']  # 在营企业手机周绝对新增
        row = [source_name, table_update_count, table_add_count, mobile_update
            , mobile_add, mobile_abs_add, zy_mobile_update, zy_mobile_add, zy_mobile_abs_add
               ]
        rows.append(row)

file_nmae_source_map = {
    # 'b2b', 'bidding', 'qcc', 'recruiting', 'year'
    'b2b': 'b2b',
    'bidding': 'bidding',
    'qcc': 'qcc',
    'recruiting': 'zp',
    'year': 'annual_report',

}

for source in sources:
    abs_path = os.path.join(dir_path, source)
    with open(abs_path, 'r', encoding='utf-8') as f:
        res_json = json.load(f)
        week_count = res_json.get("week_count")
        week_add_count = res_json.get("week_add_count")  # 该表新增条数
        week_update_count = res_json.get("week_update_count")  # 该表更新条数
        mobile_add_count = res_json.get("mobile_add_count")  # 手机号新增条数
        mobile_update_count = res_json.get("mobile_update_count")  # 手机号更新条数
        mobile_and_nameId_add_count = res_json.get("mobile_and_nameId_add_count")  # 有手机号有nameId新增条数
        mobile_and_nameId_update_count = res_json.get("mobile_and_nameId_update_count")  # 有手机号有nameId更新条数
        mobile_and_nameId_add_dup = res_json.get("mobile_and_nameId_add_dup")
        mobile_and_nameId_update_dup = res_json.get("mobile_and_nameId_update_dup")
        into_contact_table_count = res_json.get("into_contact_table_count")  # 入联系方式表条数
        ls = [week_add_count, week_update_count, mobile_add_count, mobile_update_count,
              mobile_and_nameId_add_count, mobile_and_nameId_update_count, into_contact_table_count
              ]
        for row in rows:
            source_name = row[0]
            if file_nmae_source_map[source] == source_name:
                row.extend(ls)

# 细分源：
head_line_source = [
    '来源分类', '具体来源', '周更新库内联系方式总量', '周新增库内联系方式总量', '周更新库内手机总量', '周新增库内手机总量', '周绝对新增库内手机总量', '在营企业手机周更新',
    '在营企业手机周新增', '在营企业手机周绝对新增'
]
rows.append(head_line_source)
with open(week_report_result_path, 'r', encoding='utf-8') as f:
    week_data_json = json.load(f)
    source_result_dic = week_data_json['source_result_dic']
    tj_key = ['table_update_count', 'table_add_count', 'mobile_update', 'mobile_add',
              'mobile_abs_add', 'zy_mobile_update', 'zy_mobile_add', 'zy_mobile_abs_add'
              ]
    for table_k, v_dict in source_result_dic.items():
        source_name = table_k
        for source_key, v in v_dict.items():
            if source_key not in tj_key:
                table_update_count = v['table_update_count']  # ** 周更新库内联系方式总量：{} **
                table_add_count = v['table_add_count']  # ** 周新增库内联系方式总量：{} **
                mobile_update = v['mobile_update']  # 周更新库内手机总量
                mobile_add = v['mobile_add']  # 周新增库内手机总量
                mobile_abs_add = v['mobile_abs_add']  # 周绝对新增库内手机总量
                zy_mobile_update = v['zy_mobile_update']  # 在营企业手机周更新
                zy_mobile_add = v['zy_mobile_add']  # 在营企业手机周新增
                zy_mobile_abs_add = v['zy_mobile_abs_add']  # 在营企业手机周绝对新增

                row = [source_name, source_key, table_update_count, table_add_count, mobile_update
                    , mobile_add, mobile_abs_add, zy_mobile_update, zy_mobile_add, zy_mobile_abs_add
                       ]
                rows.append(row)

import csv

with open('week_report_a.csv', 'w', newline='', encoding='utf-8') as csvfile:
    # 获得 writer对象 delimiter是分隔符 默认为 ","
    writer = csv.writer(csvfile, delimiter=',', dialect='excel')
    # 调用 writer的 writerow方法将 test_writer_data写入 test_writer.csv文件
    for row in rows:
        writer.writerow(row)
